<template>
  <base-view :model="query" @submit="search">
    <template v-slot:params>
      <a-col :span="8">
        <a-form-model-item label="通道标识">
          <a-input
            v-model="query.dstChannelId"
            placeholder="输入通道标识 或 通道名称模糊查询"
            allow-clear
          />
        </a-form-model-item>
      </a-col>
      <a-col :span="24">
        <a-form-model-item class="search">
          <a-button type="primary" html-type="submit">搜索</a-button>
          <a-button style="margin-left: 8px" html-type="reset" @click="reset">重置</a-button>
        </a-form-model-item>
      </a-col>
    </template>
    <div class="section">
      <a-table
        ref="table"
        :columns="columns"
        :dataSource="dataSource"
        :rowKey="
          (record, index) => {
            return index;
          }
        "

        :loading="loading"
        :pagination="pagination"
        :scroll="{ x: 2400 }"
        @change="pageChange"
        @showSizeChange="pageChange"
      >
        <template slot="operation" slot-scope="text, record">
          <a-icon type="setting" theme="twoTone" twoToneColor="#4a9ff5" @click="edit(record)" title="修改" v-has-permission="['pDstChannel:update']"></a-icon>
          <a-icon type="eye" theme="twoTone" twoToneColor="#4a9ff5" @click="view(record)" title="详情" v-has-permission="['pDstChannel:view']"></a-icon>
          <a-icon type="check-circle" v-hasPermission="['pDstChannel:update']" v-if="record.dstChannelStatus !== 'Y'" @click="stop(record)" theme="twoTone" twoToneColor="#4a9ff5" title="启用"></a-icon>
          <a-icon type="minus-circle" v-if="record.dstChannelStatus !== 'N'" v-hasPermission="['pDstChannel:update']" @click="stop(record)" theme="twoTone" twoToneColor="#4a9ff5" title="停用"></a-icon>
          <a-badge
            status="warning"
            text="无权限"
            v-has-no-permission="['pDstChannel:view', 'pDstChannel:update']"
          ></a-badge>
        </template>
      </a-table>
    </div>
    <a-modal
      v-model="modalVisible"
      :title="modalTitle"
      width="1050px"
      @ok="submit"
      :maskClosable="false"
      :footer="null"
    >
      <p-dst-channel-operation ref="modalForm"></p-dst-channel-operation>
    </a-modal>
  </base-view>
</template>

<script>
import PDstChannelOperation from "./PDstChannelOperation";

export default {
  name: "PDstChannel",
  components: { PDstChannelOperation },
  provide: function () {
    return {
      PDstChannelContext: this,
    };
  },
  data: function () {
    return {
      beanInfo: {},
      modalVisible: false,
      modalTitle: "",
      operationType: "view",
      query: {instId :'', dstChannelId: "", dstChannelName: "" },
      loading: false,
      pagination: {
        pageNum: 1,
        pageSize: 10,
        pageSizeOptions: ["10", "20", "30", "40", "100"],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) =>
          `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`,
      },
      columns: [
        {
          title: "机构标识",
          dataIndex: "instId",
          width: 100,
        },
        {
          title: "通道标识",
          dataIndex: "dstChannelId",
          width: 100,
        },
        {
          title: "通道名称",
          dataIndex: "dstChannelName",
          width: 200,
        },
        {
          title: "通道状态",
          dataIndex: "dstChannelStatus",
          width: 120,
          customRender: (text, row, index) => {
            switch (text) {
              case "Y":
                return "启用";
              case "N":
                return "停用";
              default:
                return text;
            }
          },
        },
        {
          title: "熔断控制开关",
          dataIndex: "fusingEnableFlag",
          width: 150,
          customRender: (text, row, index) => {
            switch (text) {
              case "Y":
                return "开启";
              case "N":
                return "关闭";
              default:
                return text;
            }
          },
        },
        {
          title: "营业开始日期",
          dataIndex: "bizStartDate",
          width: 150,
        },
        {
          title: "业务控制规则",
          dataIndex: "bizCtrlRule",
          width: 150,
          customRender: (text, row, index) => {
            switch (text) {
              case "0":
                return "按自然日";
              case "1":
                return "按工作日";
              default:
                return text;
            }
          },
        },
        {
          title: "营业开始时间",
          dataIndex: "bizStartTime",
          width: 150,
        },
        {
          title: "营业截止时间",
          dataIndex: "bizEndTime",
          width: 150,
        },
        {
          title: "是否支持智能路由",
          dataIndex: "intelligentRouteFlag",
          width: 200,
          customRender: (text, row, index) => {
            switch (text) {
              case "Y":
                return "支持";
              case "N":
                return "不支持";
            }
          },
        },
        {
          title: "交易延迟类型",
          dataIndex: "txnDelayType",
          width: 150,
          customRender: (text, row, index) => {
            switch (text) {
              case "1":
                return "实时到账";
              case "2":
                return "普通到账";
              case "3":
                return "24小时到账";
              default:
                return text;
            }
          },
        },
        {
          title: "交易最大并发数",
          dataIndex: "maxSupportNum",
          width: 150,
        },
        {
          title: "系统安全计划",
          dataIndex: "secPlanId",
          width: 150,
        },
        {
          title: "创建时间",
          dataIndex: "createTime",
          width: 200,
        },
        {
          title: "修改时间",
          dataIndex: "updateTime",
          width: 200,
        },
        {
          title: "操作",
          fixed: "right",
          scopedSlots: { customRender: "operation" },
        },
      ],
      dataSource: [],
    };
  },
  methods: {
    async submit() {
      const res = await this.$refs.modalForm.submit();
      if (res) {
        this.modalVisible = false;
        this.pagination.defaultCurrent = 1;
        await this.search();
      }
    },
    async reset(){
      let iId =this.query.instId
      this.query = {}
      this.query.instId = iId
      this.pagination.pageNum = this.pagination.defaultCurrent;
      this.pagination.pageSize = this.pagination.defaultPageSize;
      await this.search();
    },
    async search(e) {
      e && e.preventDefault && e.preventDefault();
      try {
        const res = await this.$get("/admin/igas/pDstChannelInfo", {
          instId: this.query.instId,
          dstChannelId: this.query.dstChannelId,
          pageNum: this.pagination.pageNum,
          pageSize: this.pagination.pageSize,
        });
        this.dataSource = res.data.rows;
        this.$set(this.pagination, "total", +res.data.total);
      } catch (e) {
        console.error(e);
      }
    },
    openModal(type = "add", beanInfo) {
      // 支付管理  弹窗方法
      this.modalTitle = {
        add: "新增",
        edit: "编辑",
        view: "详情",
      }[type];
      this.beanInfo = beanInfo;
      this.modalVisible = true;
      this.$nextTick(() => {
        this.$refs.modalForm.setOperatorType(type);
        this.$refs.modalForm.setBeanInfo(beanInfo);
        this.$refs.modalForm.resetFields();
        this.$refs.modalForm.initTabs(); // 调用子组件的初始化方法
      });
    },
    async edit(item) {
      if (item.dstChannelStatus=="N"){
        this.$message.error("已停用的数据不能修改")
      }else {
        this.openModal("edit", item);
      }
    },
    view(item) {
      this.openModal("view", item);
    },
    async stop(record) {
      const res = await this.$put(
        "/admin/igas/pDstChannelInfo/changeStatus",
        record
      );
      if (res.status == 200) {
        this.$message.success("保存成功！");
        this.search();
        return true;
      } else {
        this.$message.error(res.data.message);
        return false;
      }
    },
    pageChange(e) {
      this.pagination.pageNum = e.current;
      this.pagination.pageSize = e.pageSize;
      this.search();
    },
  },
};
</script>
<style lang="less" scoped>
@import "../../../../../static/less/Common";
</style>
